﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS.AdminCP
{
    public partial class DoiTac : System.Web.UI.Page
    {
        cmsDoiTacDO objDoiTac = new cmsDoiTacDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            LoadConfig();
        }
        private void LoadConfig()
        {
            gvConfig.DataSource = new cmsDoiTacBL().SelectAll();
            gvConfig.DataBind();
            pnview.Visible = true;
            pnupdate.Visible = false;
        }

        protected void gvConfig_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvConfig.PageIndex = e.NewPageIndex;
            LoadConfig();
        }

        protected void gvConfig_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                new cmsDoiTacBL().Delete(new cmsDoiTacDO { DoiTacID = Convert.ToInt32(gvConfig.DataKeys[e.RowIndex].Value) });
                Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
            }
            catch
            {
                Functions.Alert("Có lỗi xảy ra!", Request.Url.ToString());
            }
        }

        //protected void gvConfig_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    int DoiTacID = Convert.ToInt32(gvConfig.DataKeys[gvConfig.SelectedIndex].Value);
        //    Ultility.RedirectPage("Config.aspx?Page=Config&ConfigID=" + DoiTacID.ToString());
        //}

        protected void themmoi_Click(object sender, EventArgs e)
        {
            pnview.Visible = true;
            pnupdate.Visible = true;
            txtName.Focus();
            txtName.Text = "";
            txtLink.Text = "";
            Session["Id"] = "";
        }
        protected void Initobject()
        {
            objDoiTac = new cmsDoiTacBL().Select(objDoiTac);
            objDoiTac.TenDoiTac = txtName.Text;
            objDoiTac.MoTa = txtLink.Text;
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                objDoiTac.ImageUrl = UploadFile(fulImage);
            }
            
        }
        protected void lbtLuu_Click(object sender, EventArgs e)
        {
            int DoiTacId = Convert.ToInt32(gvConfig.DataKeys[gvConfig.SelectedIndex].Value);
            objDoiTac.DoiTacID = DoiTacId;
            Initobject();
            
            if (objDoiTac.DoiTacID <= 0)
            {
                new cmsDoiTacBL().Insert(objDoiTac);
                Functions.Alert("Thêm mới thành công!!");
            }
            else
            {
                new cmsDoiTacBL().Update(objDoiTac);
                Functions.Alert("Cập nhập thành công!!");
            }
            LoadConfig();
        }
        
        protected void lbtHuy_Click(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private string UploadFile(FileUpload fulImages)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtName.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImages.FileName.Substring(fulImages.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImages.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        protected void gvConfig_SelectedIndexChanged(object sender, EventArgs e)
        {
            int DoiTacId = Convert.ToInt32(gvConfig.DataKeys[gvConfig.SelectedIndex].Value);
            objDoiTac.DoiTacID = DoiTacId;
            objDoiTac = new cmsDoiTacBL().Select(objDoiTac);
            txtName.Text = objDoiTac.TenDoiTac;
            txtLink.Text = objDoiTac.MoTa;
            string images = objDoiTac.ImageUrl;
            if (!string.IsNullOrEmpty(objDoiTac.ImageUrl))
            {
                hplImage.Visible = true;
                hplImage.NavigateUrl = "/Media/" + objDoiTac.ImageUrl;
            }
            pnupdate.Visible = true;
            pnview.Visible = true;
        }
        protected void dlImages_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int DoiTacId = Convert.ToInt32(gvConfig.DataKeys[gvConfig.SelectedIndex].Value);
            new cmsDoiTacBL().Delete(new cmsDoiTacDO { DoiTacID = DoiTacId });

            Functions.Alert("Xóa thành công");
        }
        protected void gvConfig_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //int id = int.Parse(e.CommandArgument.ToString());
            //objDoiTac.DoiTacID = id;
            //Session["Id"] = id;
            //objDoiTac = new cmsDoiTacBL().Select(objDoiTac);
            //switch (e.CommandName)
            //{
            //    case "Delete":

            //        new cmsDoiTacBL().Delete(objDoiTac);
            //        Functions.Alert("Xóa bản tin thành công!");
            //        LoadConfig();
            //        break;
                //case "Select":
                //    txtName.Text = objDoiTac.TenDoiTac;
                //    txtLink.Text = objDoiTac.MoTa;
                //    string images = objDoiTac.ImageUrl;
                //    if (!string.IsNullOrEmpty(objDoiTac.ImageUrl))
                //    {
                //        hplImage.Visible = true;
                //        hplImage.NavigateUrl = "/Media/" + objDoiTac.ImageUrl;
                //    }
                //    pnupdate.Visible = true;
                //    pnview.Visible = true;
                //    break;
            //}

        }

    }
}